package com.it.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.user.domain.User;
import com.it.user.mapper.UserMapper;
import com.it.user.query.UserQuery;
import com.it.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public void addUser(User user) {
        // 生成随机盐值，通过hutool工具包生成
        String salt = RandomUtil.randomString(32);
        user.setSalt(salt);
        //通过hutool工具包实现密码加盐，然后通过md5加密
        String password = SecureUtil.md5(user.getPassword() + salt);
        user.setPassword(password);
        userMapper.addUser(user);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUser(Long id) {
        userMapper.deleteUser(id);
    }

    @Override
    public User findOne(Long id) {
        return null;
    }

    @Override
    public List<User> findAll() {
        List<User> userList = userMapper.findAll();
        return userList;
    }

    @Override
    public PageInfo<User> queryPage(UserQuery userQuery) {
        PageHelper.startPage(userQuery.getCurrentPage(), userQuery.getPageSize());
        List<User> userList = userMapper.queryPage(userQuery);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    @Override
    public void batchUsers(Long[] ids) {
        userMapper.batchUsers(ids);
    }
}
